<template>
  <div id="DepInSet_container" style="height: 98%">
    <el-card>
      <div class="searchDiv">
        <div class="searchDivLeft">
          会社名称:
          <el-select v-model="depSearch.searchbean.groupEnterpriseGuid" placeholder="全部" class="cpnSelect">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select
          >&nbsp; 部門名称: <el-input v-model="depSearch.searchbean.departName" clearable class="departInput"></el-input>&nbsp; 開発部門か否か:
          <el-select v-model="depSearch.searchbean.isKaifa" placeholder="全部" style="width: 100px">
            <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
          <el-button type="primary" v-preventReClick class="search" icon="el-icon-search" @click="searchDep">検索</el-button>
        </div>
        <div class="searchDivRight">
          <el-button type="primary" v-preventReClick icon="el-icon-plus" @click="dialogFormVisible = true">新規</el-button>
          <el-button type="warning" v-preventReClick icon="el-icon-edit" @click="updataBtn">修正</el-button>
          <el-button type="danger" v-preventReClick icon="el-icon-delete" @click="remove">削除</el-button>
        </div>
      </div>
      <el-table
        :data="depList"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange"
        border
        style="width: 100%; height: 100%"
        class="mainTable"
        :header-cell-style="{ background: '#f2f2f2', color: '#606266' }"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="会社名称" prop="undefined"> </el-table-column>
        <el-table-column prop="departNo" label="部門番号"> </el-table-column>
        <el-table-column prop="departName" label="部門名称"> </el-table-column>
        <el-table-column prop="isKaifa" label="開発部門か否か"> </el-table-column>
        <el-table-column prop="sortFlag" label="部門  ソート順"> </el-table-column>
      </el-table>
      <!-- 分页部分 -->
      <div class="block" style="margin-bottom: 0; margin: auto; text-align: center; margin-top: 50px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="depSearch.indexPage"
          :page-size="depSearch.pageSize"
          layout="total, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <!-- 新增功能部分 -->
      <el-dialog center @close="resetForm('ruleForm')" title="部門情報新規" v-model="dialogFormVisible" width="40%">
        <!-- 内容主题区域 -->
        <div class="add_container">
          <!-- 表单部分 -->
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm formSet formStyle">
            <el-form-item label="会社名称" prop="groupEnterpriseGuid" :label-width="formLabelWidth">
              <el-select v-model="ruleForm.groupEnterpriseGuid" placeholder="会社を選んでください" style="width: 100%">
                <el-option v-for="item in options3" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="部門名称" prop="departName" :label-width="formLabelWidth">
              <el-input v-model="ruleForm.departName" placeholder="部門名称を入力してください" maxlength="50"></el-input>
            </el-form-item>
            <el-form-item label="部門番号" prop="departNo" :label-width="formLabelWidth">
              <el-input v-model="ruleForm.departNo" placeholder="部門番号を入力してください" maxlength="4"></el-input>
            </el-form-item>
            <el-form-item label="部門ソート順" prop="sortFlag" :label-width="formLabelWidth">
              <el-input v-model.number="ruleForm.sortFlag" placeholder="部門ソート順を入力してください" maxlength="3"></el-input>
            </el-form-item>
            <el-form-item label="開発部門か否か" prop="isKaifa" :label-width="formLabelWidth">
              <el-radio-group v-model="ruleForm.isKaifa">
                <el-radio :label="'1'">はい</el-radio>
                <el-radio :label="'0'">いいえ</el-radio>
              </el-radio-group>
            </el-form-item>

            <div class="centerSet">
              <el-button type="primary" @click="submitForm" v-preventReClick>保存</el-button>
              <el-button @click="resetForm('ruleForm')">リセット</el-button>
            </div>
          </el-form>
        </div>
      </el-dialog>
      <!-- 编辑功能部分 -->
      <el-dialog @close="resetForm('editruleForm')" title="部門情報編集" v-model="editFormVisible" width="40%">
        <!-- 内容主题区域 -->
        <div class="add_container">
          <!-- 表单部分 -->
          <el-form :model="editruleForm" :rules="rules" ref="editruleForm" label-width="100px" class="demo-ruleForm formSet">
            <el-form-item label="会社名称" prop="groupEnterpriseGuid" :label-width="formLabelWidth">
              <el-select v-model="editruleForm.groupEnterpriseGuid" placeholder="会社を選んでください" style="width: 100%">
                <el-option v-for="item in options3" :key="item.value" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="部門名称" prop="departName" :label-width="formLabelWidth">
              <el-input v-model="editruleForm.departName" placeholder="部門名称を入力してください" maxlength="50"></el-input>
            </el-form-item>
            <el-form-item label="部門番号" prop="departNo" :label-width="formLabelWidth">
              <el-input v-model="editruleForm.departNo" placeholder="部門番号を入力してください" maxlength="4"></el-input>
            </el-form-item>
            <el-form-item label="部門ソート順" prop="sortFlag" :label-width="formLabelWidth">
              <el-input v-model.number="editruleForm.sortFlag" placeholder="部門ソート順を入力してください" maxlength="3"></el-input>
            </el-form-item>
            <el-form-item label="開発部門か否か" prop="isKaifa" :label-width="formLabelWidth">
              <el-radio-group v-model="editruleForm.isKaifa">
                <el-radio :label="'1'">はい</el-radio>
                <el-radio :label="'0'">いいえ</el-radio>
              </el-radio-group>
            </el-form-item>

            <div class="centerSet">
              <el-button type="primary" @click="editsubmitForm" v-preventReClick>保存</el-button>
              <el-button @click="resetForm('editruleForm')">リセット</el-button>
            </div>
          </el-form>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 公司列表数据
      enterPrise: [],
      formLabelWidth: '135px',
      // 复选框选中数据
      multipleSelection: [],
      // 搜索提交的数据
      depSearch: {
        indexPage: 1,
        pageSize: 8,
        searchbean: {
          departName: '',
          groupEnterpriseGuid: '',
          isKaifa: ''
        }
      },
      total: 0,
      options: [
        {
          value: '',
          label: '全部'
        }
      ],
      options1: [
        {
          value: '',
          label: '全部'
        },
        {
          value: '1',
          label: 'はい'
        },
        {
          value: '0',
          label: 'いいえ'
        }
      ],
      options2: [],
      options3: [],
      depList: [],
      dialogFormVisible: false,
      editFormVisible: false,
      // 表单规则校验
      rules: {
        groupEnterpriseGuid: [{ required: true, message: '会社の名前を選んでください', trigger: 'blur' }],
        departName: [{ required: true, message: '部門名称を入力してください', trigger: 'blur' }],
        departNo: [{ required: true, message: '部門番号を入力してください', trigger: 'blur' }],
        sortFlag: [{ required: true, type: 'number', message: '０～９９９の数字を入力してください', pattern: /^[1-9]\d{1,2}|\d$/, trigger: 'blur' }],
        isKaifa: [{ required: true, message: '開発部か否かを選んでください', trigger: 'blur' }]
      },
      // 表单绑定数据
      ruleForm: {
        groupEnterpriseGuid: '',
        departName: '',
        departNo: '',
        isKaifa: '',
        sortFlag: '',
        color: ''
      },
      //
      editruleForm: {},
      ruleFormCopy: {
        groupEnterpriseGuid: '',
        departName: '',
        departNo: '',
        isKaifa: '',
        sortFlag: '',
        color: ''
      },
      removeIDS: {
        departGuids: []
      }
    }
  },
  created() {
    this.enterpriseName()
    this.searchDep()
  },
  mounted() {},
  methods: {
    // 获取所有公司
    enterpriseName() {
      // 聲明對象
      function ObjStory(value, label) {
        this.value = value
        this.label = label
      }
      this.axios.post('/role/selectAllEnterprise').then((res) => {
        this.enterPrise = res.data.data
        //  console.log(this.enterPrise)
        this.enterPrise.forEach((item) => {
          this.options.push(new ObjStory(item.groupEnterpriseGuid, item.enterpriseName))
          this.options3.push(new ObjStory(item.groupEnterpriseGuid, item.enterpriseName))
          this.options2 = this.options
        })
      })
    },
    // 复选框选中方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 分页方法 pagesize发生改变
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.depSearch.pageSize = newSize
      this.searchDep()
    },
    // 当前页
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.depSearch.indexPage = newPage
      this.searchDep()
    },
    // 搜索按键功能
    searchDep() {
      console.log(this.depSearch)
      this.axios
        .post('/department/select/list', this.depSearch)
        .then((res) => {
          this.depList = res.data.data.list
          this.depList.forEach((item) => {
            if (item.isKaifa === '1') item.isKaifa = 'はい'
            else if (item.isKaifa === '0') item.isKaifa = 'いいえ'
            this.options.forEach((ele) => {
              const groupEnterpriseName = groupEnterpriseName
              if (item.groupEnterpriseGuid === ele.value) item[groupEnterpriseName] = ele.label
            })
          })
          console.log(this.depList)
          this.total = res.data.data.total
        })
        .catch((error) => {
          console.log(error)
        })
    },
    // 提交新增以及重置
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          console.log(this.ruleForm)
          this.ruleForm.departName = this.ruleForm.departName.trim()
          this.ruleForm.isKaifa = this.ruleForm.isKaifa.trim()
          this.axios
            .post('/department/insert', this.ruleForm)
            .then((res) => {
              if (res.data.success !== 1) {
                this.ruleForm.isKaifa = ''
                return this.$message.error(res.data.msg)
              }
              this.$message.success(this.$getMsg('I0001'))
              this.ruleForm = this.ruleFormCopy
              this.dialogFormVisible = false
              this.searchDep()
            })
            .catch((error) => {
              this.ruleForm.isKaifa = ''
              console.log(error)
            })
        }
      })
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 编辑按钮事件
    updataBtn() {
      if (this.multipleSelection.length !== 1)
        return this.$message({
          showClose: true,
          message: this.$getMsg('W0001'),
          type: 'warning'
        })
      else this.editFormVisible = true
      this.axios
        .post('/department/select', {
          departGuid: this.multipleSelection[0].departGuid
        })
        .then((res) => {
          this.editruleForm = res.data.data
          console.log(this.editruleForm)
          this.editruleForm.sortFlag = this.editruleForm.sortFlag
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 编辑更新事件
    editsubmitForm() {
      console.log(this.editruleForm)
      this.$refs.editruleForm.validate((valid) => {
        if (valid) {
          this.axios
            .put('/department/update', this.editruleForm)
            .then((res) => {
              if (res.data.success !== 1) {
                this.ruleForm.isKaifa = ''
                return this.$message.error(res.data.msg)
              }
              this.$message.success(this.$getMsg('I0002'))
              this.editFormVisible = false
              this.editruleForm = this.ruleFormCopy
              this.searchDep()
            })
            .catch((err) => {
              this.ruleForm.isKaifa = ''
              console.log(err)
            })
        }
      })
    },
    // 删除按钮事件
    remove() {
      this.removeIDS.departGuids = []
      if (this.multipleSelection.length === 0) {
        return this.$message({
          showClose: true,
          message: this.$getMsg('W0002'),
          type: 'warning'
        })
      }
      console.log(this.multipleSelection)
      this.multipleSelection.forEach((item) => {
        this.removeIDS.departGuids.push(item.departGuid)
      })
      console.log(this.removeIDS)
      this.$confirm(this.$getMsg('I0004'), {
        cancelButtonText: 'キャンセル',
        confirmButtonText: '確認',
        type: 'warning'
      }).then(
        () => {
          this.axios
            .delete('/department/delete', { data: this.removeIDS })
            .then(() => {
              this.$message.success(this.$getMsg('I0003'))
              this.searchDep()
              this.removeIDS.departGuids = []
            })
            .catch((error) => {
              console.log(error)
            })
        },
        (res) => {
          console.log(res)
        }
      )
    },
    resetForm(formName) {
      //重置form表单 フォームをリセット
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
.top_search .el-input {
  width: auto;
}
/* 新增弹出框 */
#DepInSet_container .add_container {
  width: 100%;
  height: 350px;
}
.formSet .el-form-item {
  margin-right: 30px;
  margin-bottom: 20px;
}
.formSet .el-radio-group {
  margin-left: 20px;
}
.formSet .picker {
  margin-left: 20px;
}
#DepInSet_container .el-form .el-form-item {
  margin-right: 30px;
}
.centerSet {
  width: 100%;
  text-align: center;
}
</style>
